ieeenjitfandomcom-20200213-history
ECE 252
Material created by Gabriel Nieves, Former IEEE NJIT Student Branch member and HKN Gamma Kappa President 2011-2012. ---- Editor's Note: This guide was created to provide you with additional support material for an ECE class and/or software. The supplemental material is not meant to replace nor cover all of topics given in the class. All course books, class handouts, and software tutorials should still be read as required. You may contact us at ieee.njit@gmail.com for more information on the IEEE NJIT Student Branch Wiki. Please support the student project by reporting any errors in the guide or contributing your own material. __TOC__ Introduction The main purpose of this guide is to reinforce some of the lessons learned in the ECE 252 course on microprocessors. Many of these concepts are illustrated by examples using the EASy68K software. EASy68K is an all-in-one editor, assembler, and simulator for the Motorola 68000 microprocessor. The software replaces the DOS-based emu68k.exe and asm68k.exe with an elegant graphical interface and other useful features. While both assemblers are based off the same instruction set, the same program may not cleanly assemble on both EASy68K and asm68k.exe. There may be different syntax or behavior for some assembler directives or instructions (e.g. TRAPs), requiring small changes when converting to a different assembler. Some important links for EASy68K are provided below: EASy68K Home Page & Installation EASy68K Quick Reference EASy68K Quick Start Guide (Short Tutorial) Please Download EASy68K. You will have a difficult time learning the material if you are not using software well. You should still experiment by making your own changes to the EASy68K sample programs during your free time. 'Hello World' Program Basic Features of EASy68K In Fig. 1, you can already see some interesting features of the editor. All of the code is automatically color-coded by the editor to identify the purpose of the text. For example, blue text represents comments. By default, the editor’s text settings are: Comments are blue. Labels are purple. Directives are green. Strings are teal. Structured assembler codes are red. Instruction opcodes and operands are black. This feature is not perfect (the zero after “Hello World!” should not be blue) so don’t be solely dependent on the colors of the text for guidance on how your code should assemble. Another important feature is the column arrangement in the code. The first column is reserved for labels. If you do not want a label for a line, your code should not start at column one. Otherwise, you will receive an error when you assemble. The second column is for opcodes & assembler directives, the third column is for operands, and the last column is for comments. Comments, which are notes for documenting code that are not recognized by the assembler, can be designated in two ways. If an entire line is a comment, the first character of the line should be an asterisk (*). You may also precede a comment with a semicolon (;) if it does not have its own line. You can assemble the source code (X68 file) by clicking on Assemble Source (press F9 or green play button). A dialog box will open, displaying the number of errors and warnings. While warnings won’t prevent you from assembling your code, you may get unexpected results when you simulate your code. After assembling, two more files will be created. The listing file (L68 file) is a detailed recording of your source code, its representation in memory, any errors/warnings, etc. You can view the listing file after assembling by pressing “Load L68” (Fig. 2). The S-record file (S68 file) is the assembled program that the simulator reads from. After assembling with no errors, you can open up the simulator, called SIM68K, by pressing “Execute” on the dialog box. After a few moments, a new window will open with the listing file and a new interface (Fig. 3). The action bar will have a couple of icons for running, tracing (going through the code line by line), and rewinding the program. Just below the action bar, you will see your general registers (data, address) and system registers (status, user/system stack pointer, program counter). On the left margin of the source code, you will see the breakpoint bar. The green buttons represent individual instructions. You can create a breakpoint for debugging by clicking on the green button; this will cause the program to pause at that instruction when you simulate. To start the simulation, press the run button (F9 or the green play button). If your program has any I/Os, the output window should pop up. You can also display the memory, stack, or output window by clicking on the “View” menu. Step-by-Step Walkthrough The source code for the “Hello World” program is provided below. Please copy and assemble the program yourself. Follow the walkthrough using the assembled program. ORG $2000 INTROMSG DC.B 'Hello, World!', 0 ; starting address of INTROMSG ORG $3000 ; starting address of GREETING GREETING MOVEA.L #INTROMSG, A1 ; move INTROMSG to A1 MOVE.B #13, D0 ; task #13, display null-terminated string with CR, LF TRAP #15 MOVE.B #9,D0 TRAP #15 ; halt simulator END GREETING ; last line of source #The 1st line is not an instruction of the Motorola 68K but an assembler directive. ORG (origin) is a message to the assembler, telling it to start placing data from address 0x2000 in memory. # The 2nd line is storing the string of bytes in memory. The label INTROMSG points to the memory location 0x2000. The assembler directive DC.B (define constant in bytes) tells the assembler to store the string “Hello, World!”, followed by a null character (or zero byte). If you go to address 0x2000 using the memory window in SIM68K (Fig. 4), you will see the string is stored using ASCII-encoded bytes. #The 3rd line tells the assembler to start placing data starting from address 0x3000. #The 4th line is the first instruction of the program. The instruction MOVEA.L will move four bytes of the value INTROMSG ($2000) into the address register A1. On SIM68K, the first instruction is highlighted and the PC (program counter) is $3000. Execute that single instruction by pressing F8 (or “Step Over” under the “Run” menu). Now, the register A1 will have the value $2000 and the PC will now point to the next instruction at address 0x3006. #The 5th line stores a byte of the value 13 (or $D in hexadecimal) into register D0. This value in D0 is used by TRAP to determine what task to do. #The 6th line runs the TRAP routine and executes the task. In this case, task number #13 displays a null-terminated string on the output window. Once you have stepped over this instruction, an output window should pop up with your message. #The 7th line stores the value 9 into register D0. #The 8th line runs the TRAP routine again, this time telling the simulator that it has completed the program. #The 9th line tells the assembler that this is the end of the source code. No text following the assembler directive END will be recognized by assembler and will be treated as comments. The operand GREETING tells the assembler that it’s the label of the start of the program (first instruction). This last line is required for EASy68K. For more information on EASy68K assembler directives and other assembler command, read the program’s support documents through the “Help” menu on EASy68K. You can find the complete ASCII table and its representation with different radices at http://www.asciitable.com/. Data Storage Program ORG $2000 MSG1 DC.B 'Hello, World!' ORG $2010 MSG2 DC.B 'Hello' DC.B 44, 32 DC.B 'Worl', $64 DC.B '!' DC.B $0 ORG $2020 SERIES DC.B 1, 2, 3, 4 ORG $2030 DC.W 1, 2, 3, 4 HELLO1234 ORG $2040 DC.W 'Hello' ORG $2050 DC.B 1 DC.L 2, 3, 4 ORG $3000 START MOVEA.L #MSG1, A1 ; move MSG1 address to A1 MOVE.B #13, D0 ; display message TRAP #15 MOVE.B #9, D0 TRAP #15 ; halt simulator END START ; last line of source Bit/Byte Endianess It is important to understand how bytes (and bits) are ordered in memory. For the Motorola 68000 architecture, memory storage uses big-endian for the bytes. Big-endian is when the most significant byte is placed first in memory. Figure 5 shows data stored in memory using both big-endian and little-endian. The bit endianness for Motorola 68K is also big-endian, meaning the most significant bit is located on the far left. For a byte, the bits are from 7 to 0 (reading from left to right). Data Size & type In the data storage program, you see that we are starting to store more than just strings into memory. Despite looking very different in code, MSG1 and MSG2 have the same content. The latter message has some of its bytes being stored using different data types. You can see both messages in memory in Figure 6. The space and exclamation point in MSG2 are stored into memory as decimal values. Those values are the ASCII-encoded bytes in decimal. Lower-case d is also stored as a number, but as a hexadecimal value. All hexadecimal values are preceded by a dollar sign ($). All data in memory or registers are represented as hexadecimal values, no matter how they are stored in code. You have the option to add the size suffix on some opcodes. For Motorola 68K, B stands for byte (8-bits), W stands for word (16-bits), and L stands for longword (32-bits). In the data storage program, two sets of numbers are stored into memory at 0x2020 and 0x2030. The first set is stored as bytes, taking up only one byte per value. The second set is stored as words, taking up two bytes per value. Even if the value can be represented with just one byte ($01), suffix will force the value to take up two bytes ($0001). Data Alignment & Probing If the data is larger than its intended size, it will take up additional bytes, words, or longwords until the entire data is stored. Under the label HELLO1234, the string “Hello” can be represented with just five bytes. Instead, the string takes up six bytes in memory because it is stored using words (two bytes). This process is called data padding. In data alignment, the value is be given a memory offset so its data size starts at a particular address in memory. In Motorola 68K, all words and longwords must begin at an even-numbered address. In $2050, you can see the byte 1, a memory offset of $FF, then the longword 2. Without data alignment, the longword would be stored at address 0x2051. Both data padding and data alignment allows the memory system to be optimized in the Motorola 68K. This is also the reason why the directive ORG must always point to an even-numbered address. If you begin at an odd-numbered address in code, the assembler will give a warning and force the data to the next even-numbered address. Null Character When you run the program, the output window will not print just the first message (Fig. 7). There is a missing zero byte stored after the first message. For an output displaying a null-terminated string, the printing routine will continue to display bytes until it finally reaches a null character. In this case, it prints out some garbage $FF bytes and the second message until it finds a null character. If there are no zero bytes in memory after your message, the assembler will continue to print garbage and possibly crash. Reverse String Program ORG $2000 REVERSE MOVEA.L #LOC1, A3 MOVEA.L #LOC2, A2 CLR.L D1 *Calculate the size of LOC1 LOC1SIZE MOVE.B 0(A3, D1), D2 CMPI.B #0, D2 BEQ REVERSEMSG ADDI.B #1, D1 BRA LOC1SIZE *Starting from the end of LOC1's message, start moving characters into LOC2 REVERSEMSG SUBI.B #1, D1 MOVE.B 0(A3, D1), D4 MOVE.B D4, (A2)+ CMPI.B #0, D1 BNE REVERSEMSG MOVE.B #0, (A2) ;Insert null byte at end of LOC2's message MOVEA.L #LOC2, A1 MOVE.B #13, D0 TRAP #15 MOVE.B #9, D0 TRAP #15 ORG $2200 LOC1 DC.B 'This tutorial is fantastic!',0 ORG $2400 LOC2 DS.B 80 END REVERSE Memory Addressing (ORG and Labels) The addresses of data and instructions residing in memory are just as important as the code itself. In the reverse string program, the data is located higher in memory (0x2200) than the instructions (0x2000). Because both data and instructions are assembled before the program is simulated, it does not matter which is stored first. The two most important things about assigning origins are avoiding overlapping data & instructions and reserved memory. When using ORG, you should make sure that you are not reusing a memory location that was already written into in your previous code. In complex programs, the portion of the memory may also be reserved for stack memory or system initialization. Status Register The status register is divided into two parts. The lower byte, or user byte, of the status register records the behavior of the previous instructions. The different possible behavior states, or flags, are: X – Extend: an extend bit from a carry for precision purposes. N – Negative: a negative result is produced. Z – Zero: a result of zero is produced. V – Overflow: an arithmetic overflow is generated C – Carry: an arithmetic carry or borrow is generated Most instructions will affect at least some of these flags after they are executed. For example, an ADD instruction between two numbers that result in a zero will change the Z-flag to one, while the other flags will become zero. You will need to look at the Motorola 68K instruction set documentation (from online or book) to see the complete list of instructions and the flags affected. The upper byte of the status register is called the system byte. It is used for denoting trace mode, supervisor state, and interrupt mask. These advanced settings can only be modified in supervisor mode. A screenshot of the SIM68K’s status register is displayed as Figure 8.